<template>
  <div class="address-list">
    <nav-bar title="地址管理"> </nav-bar>

    <ul class="list">
      <li
        class="item"
        v-for="item in addressList"
        :key="item.id"
      >
        <div class="left">
          <p class="name">{{ item.name }}</p>
          <div class="icon" v-if="item.is_default">默认</div>
        </div>
        <div class="middle" @click="selectAddress(item.id)">
          <p class="phone">{{ item.mobile }}</p>
          <p class="area">{{ item.address }}{{ item.address_detail }}</p>
        </div>
        <div class="right">
          <img
            src="http://jinglins.gitee.io/legou/img/edit.d508a455.png"
            alt=""
            @click="toChangeAddress(item.id)"
          />
        </div>
      </li>
    </ul>

    <div class="add-address">
      <div class="add-btn" @click="toAddAddress">+ 新建地址</div>
    </div>
  </div>
</template>

<script>
import { getAddressList } from "@/api/pages/address";
import NavBar from "@/components/common/nav-bar";
export default {
  data() {
    return {
      addressList: [],
    };
  },
  methods: {
    // 获取地址列表
    getListData() {
      let openId = localStorage.getItem("openId");
      getAddressList({ openId }).then((res) => {
        this.addressList = res.data;
      });
    },
    // 跳转到添加地址界面
    toAddAddress() {
      this.$router.push("/pages/addAddress");
    },
    // 跳转到修改地址界面
    toChangeAddress(id) {
      this.$router.push({ path: "/pages/addAddress", query: { id } });
    },

    // 选择地址
    selectAddress(id) {
      this.$store.commit("setAddressId", id);
      this.$toast.success('选择收货地址成功');
      this.$router.go(-1);
    },
  },
  created() {
    this.getListData();
  },
  components: {
    "nav-bar": NavBar,
  },
};
</script>

<style lang="scss">
.address-list {
  .list {
    .item {
      box-sizing: border-box;
      display: flex;
      padding: 15px;
      height: 95px;
      width: 100%;
      border-bottom: 1px solid #d9d9d9;
      background-color: #fff;
      .left {
        width: 20%;
        height: 100%;
        padding-top: 12px;
        .icon {
          margin-top: 10px;
          height: 15px;
          width: 30px;
          line-height: 15px;
          text-align: center;
          color: #b4282d;
          border: 1px solid #b4282d;
        }
      }
      .middle {
        width: 65%;
        padding-top: 10px;
        .area {
          margin-top: 10px;
        }
      }
      .right {
        width: 15%;
        display: flex;
        img {
          display: block;
          margin: auto;
          height: 25px;
          height: 25px;
        }
      }
    }
  }
  .add-address {
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 1;
    box-sizing: border-box;
    padding: 15px 10px;
    height: 67px;
    width: 100%;
    background-color: #fff;
    .add-btn {
      height: 100%;
      width: 100%;
      border: 1px solid #b4282d;
      text-align: center;
      color: #b4282d;
      line-height: 37px;
    }
  }
}
</style>